using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

using CYZHAPI.Common;
using CYZHAPI.Entity;
using CYZHAPI.IRepository.IService;
using CYZHAPI.Middlewares;

using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using CYZHAPI.web.Model;
using Microsoft.Extensions.Options;
using CYZHAPI.Common.Config;
namespace CYZHAPI.web.Controllers
{
    public class modisController : BaseController
    {
         private WebApiOption _Config;
        private IModisService _modisService;
        public modisController(IOptions<WebApiOption> option,IModisService modisService, ILogger<BaseController> logger) : base(logger)
        {
            _Config = option.Value;
            _modisService = modisService;
        }

        [HttpGet("{id}")]
        public ModisRenBean Get(string id)
        {
            return null;
        }

        [Route("Seach")]
        [HttpPost]
        public ModisRenBean Seach([FromBody]ModisSeachBean value)
        {
            value.dataformat = value.dataformat == null ? "" : value.dataformat;
            value.rowcol = value.rowcol == null ? "" : value.rowcol;
            value.fileName = value.fileName == null ? "" : value.fileName;
            value.beginTime = value.beginTime == null ? "" : value.beginTime;
            value.endTime = value.endTime == null ? "" : value.endTime;
            value.mtype = value.mtype == null ? "" : value.mtype;
            string sWhere = "";
            if (!value.dataformat.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "DATAFORMAT = '" + value.dataformat + "'" : " and DATAFORMAT = '" + value.dataformat + "'";
            }

            if (!value.rowcol.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "SCOPE = '" + value.rowcol + "'" : " and SCOPE = '" + value.rowcol + "'";
            }

            if (!value.fileName.Equals(""))
            {
                //sWhere += sWhere.Equals("") ? "FILENAME = '"+value.fileName+"'" : " and FILENAME = '"+value.fileName+"'";
                sWhere += sWhere.Equals("") ? "FILENAME like '%" + value.fileName + "%'" : " and FILENAME like '%" + value.fileName + "%'";
            }

            if (!value.beginTime.Equals(""))
            {
                //DateTime BTime = Convert.ToDateTime(value.beginTime);
                sWhere += sWhere.Equals("") ? "FILETIME > '" + value.beginTime + "'" : " and FILETIME > '" + value.beginTime + "'";
            }

            if (!value.endTime.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "FILETIME < '" + value.endTime + "'" : " and FILETIME < '" + value.endTime + "'";
            }

            if (!value.mtype.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "MTYPE = '" + value.mtype + "'" : " and MTYPE = '" + value.mtype + "'";
            }
             if(sWhere.Equals("")){
                sWhere +="id>0";
            }
            List<ModisModel> ListInfo = _modisService.GetList(sWhere);
            ModisRenBean renBean = new ModisRenBean();
            if (ListInfo != null)
            {
                renBean.code = "200";
                renBean.message = "modis数据查询成功";
                renBean.reList = ListInfo;
            }
            else
            {
                renBean.code = "901";
                renBean.message = "modis数据查询失败";
                renBean.reList = null;
            }
            return renBean;
        }

        [Route("SeachModisSrc")]
        [HttpPost]
        public ModisRenBean SeachModisSrc([FromBody]ModisSeachBean value)
        {
            string sWhere = "a.rowcol in b.rowcol  ";
            if (!String.IsNullOrEmpty(value.mtype))
            {
                sWhere += " and a.mtype='" + value.mtype + "'" ;
            }
            if (!String.IsNullOrEmpty(value.XzqhCode))
            {
                sWhere += " and b.xzqhcode='" + value.XzqhCode + "'" ;
            }
            if (!String.IsNullOrEmpty(value.year))
            {
                sWhere += " and a.year=" + value.year ;
            }
            if (!String.IsNullOrEmpty(value.days))
            {
                sWhere += " and a.days=" + value.days ;
            }

            List<ModisModel> ListInfo = _modisService.GetListUninXZQH(sWhere);
            ModisRenBean renBean = new ModisRenBean();
            if (ListInfo != null)
            {
                renBean.code = "200";
                renBean.message = "modis数据查询成功";
                renBean.reList = ListInfo;
            }
            else
            {
                renBean.code = "901";
                renBean.message = "modis数据查询失败";
                renBean.reList = null;
            }
            return renBean;
        }
        [Route("SeachModisRes")]
        [HttpPost]
        public ModisRenBean SeachModisRes([FromBody]ModisSeachBean value)
        {
            value.XzqhCode=value.XzqhCode == null ? "" : value.XzqhCode;
            value.year=value.year == null ? "" : value.year;
            value.days=value.days == null ? "" : value.days;
            value.algorithm=value.algorithm == null ? "" : value.algorithm;
             
            string sWhere = "";
            
            if (!value.XzqhCode.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "SCOPE = '" + value.XzqhCode + "'" : " and SCOPE = '" + value.XzqhCode + "'";
            }

            if (!value.year.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "year = '"+value.year+"'" : " and year = '"+value.year+"'";
            }
            if (!value.days.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "days = '"+value.days+"'" : " and days = '"+value.days+"'";
            }
            if (!value.algorithm.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "algorithm = '"+value.algorithm+"'" : " and algorithm = '"+value.algorithm+"'";
            }


            List<ModisResultsModel> ListInfo = _modisService.GetMosisResultsList(sWhere);
            ModisRenBean renBean = new ModisRenBean();
            if (ListInfo != null)
            {
                renBean.code = "200";
                renBean.message = "modis数据查询成功";
                renBean.modresList = ListInfo;
            }
            else
            {
                renBean.code = "901";
                renBean.message = "modis数据查询失败";
                renBean.modresList = null;
            }
            return renBean;
        }
        [HttpPost]
        public BaseResponseResult Post([FromBody]List<ModisModel> value)
        {
            BaseResponseResult response = new BaseResponseResult();
            bool e = _modisService.insert("MODIS_DATA_DIRECTORIES", value);
            if (e)
            {
                response = new BaseResponseResult
                {
                    code = "200",
                    message = "数据库插入成功!"
                };
            }
            else
            {
                response = new BaseResponseResult
                {
                    code = "901",
                    message = "数据库插入错误!"
                };
            }

            return response;
        }

        [HttpPut("{id}")]
        public BaseResponseResult Put(long id, [FromBody]ModisModel value)
        {
            BaseResponseResult response = new BaseResponseResult();
            bool e = _modisService.update(id, value);//"ASC_DATA_DIRECTORIES"
            if (e)
            {
                response = new BaseResponseResult
                {
                    code = "200",
                    message = "数据更新成功!"
                };
            }
            else
            {
                response = new BaseResponseResult
                {
                    code = "901",
                    message = "数据更新错误!"
                };
            }

            return response;
        }

        // DELETE api/values/5
        [Route("Delete")]
        [HttpGet("{id}")]
        public BaseResponseResult Delete(int id)
        {
            BaseResponseResult response = new BaseResponseResult();
            bool e = _modisService.Delete(id);//"ASC_DATA_DIRECTORIES"
            if (e)
            {
                response = new BaseResponseResult
                {
                    code = "200",
                    message = "数据删除成功!"
                };
            }
            else
            {
                response = new BaseResponseResult
                {
                    code = "901",
                    message = "数据删除错误!"
                };
            }
            return response;
        }
         [Route("downLoadFile")]
        [HttpGet]
        public async Task<IActionResult> downLoadFile(string fileType,string fileid,string year){
            
            var sWhere="id="+fileid;
            var path=_Config.Path.basePath;
            List<ModisModel> ListInfo = _modisService.GetList(sWhere);
            if(ListInfo != null&&ListInfo.Count==1){
                ModisModel srm=ListInfo[0];
                
                 var addrUrl=path+fileType+"\\source\\"+year+"\\"+srm.filename+".hdf";
                var stream = System.IO.File.OpenRead(addrUrl);
                return File(stream, "application/octet-stream", srm.filename+".hdf");
            }else{
                return null;
            }
           
        }
    }
}